package cn.tedu.tmall.front.mall.dao.search;

import cn.tedu.tmall.common.pojo.po.GoodsSearchPO;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodsElasticserchRepository extends ElasticsearchRepository<GoodsSearchPO , Long> {
    /*// 简单的搜索
    List<GoodsSearchPO> queryByTitle(String keyword);
    List<GoodsSearchPO> queryByBrief(String keyword);

    // 对搜索的结果进行分页
    SearchPage<GoodsSearchPO> queryByTitle(String keyword, Pageable pageable);*/

    // 自定义搜索
    @Query("{\n" +
            "    \"bool\": {\n" +
            "      \"should\": [\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"title\": \"?0\"\n" +
            "          }\n" +
            "        },\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"brief\": \"?0\"\n" +
            "          }\n" +
            "        },\n" +
            "        {\n" +
            "          \"match\": {\n" +
            "            \"keywods\": \"?0\"\n" +
            "          }\n" +
            "        }\n" +
            "      ]\n" +
            "    }\n" +
            "  }")
    @Highlight(fields = @HighlightField(name = "title"),
               parameters = @HighlightParameters(preTags = "<font style='color: red;'>",postTags = "</font>"))
    SearchPage<GoodsSearchPO> customSearch(String keyword, Pageable pageable);
}
